#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   example_request.py
@Time    :   2021-10-20 21:57:29
@Author  :   GuoLiuFang
@Version :   0.1
@Contact :   909104374@qq.com
@License :   (C)Copyright 2018-2021, RandomMatrix
@Desc    :   Perform test request
'''

# here put the import lib
# import pprint
import json
import os
import requests

# 使用说明
# erro_code     1100    message     '读取二进制不成功或者图片打开不成功'
# erro_code     2200    message     '模型预测不成功'
# erro_code     2201    message     '预测结果渲染不成功'
# erro_code     2202    message     'post requst data is None'
# erro_code     2209    message     '模型未识别到任何异常或者非异常的物体'
# 
# erro_code     3300    message     '参数key缺失'
# erro_code     3301    message     '参数key存在，但参数value缺失'
# erro_code     3302    message     '参数key存在，参数value也存在，但是数据格式不合规'
# erro_code     3309    message     '无电子围栏设置，请设置电子围栏'
BASE_URL = "http://localhost:8501"
def test_ele_fence_example():
    DETECTION_URL = BASE_URL + "/v1/abnormal-detection/electronic-fence"
    TEST_IMAGE = "/app/20211014113026.jpg"

    image_data = open(TEST_IMAGE, "rb").read()
    data = {
        "ImageName": os.path.basename(TEST_IMAGE), # 图像名称，必填，用于获取结果。
        "Base64": False, # 是否返回图像base64, 默认True
        "RenderURL": True, # 是否返回检测结果渲染后的图像URL, 默认False, 因为写硬盘耽误接口时间
        # "ResultSaveDays": 3, # 结果图片保留时长, 默认3天，大于3天删除
        "ele_fence_key": 'sn1', # 选择合适的电子围栏坐标
    }
    # files 字段是必选择的
    response = requests.post(DETECTION_URL, files={"image": image_data}, data=data).json()

    # pprint.pprint(response)
    print(json.dumps(response, indent=4))


def test_outside_example():
# outside examples begin
    DETECTION_URL = BASE_URL + "/v1/object-detection/person-car"
    TEST_IMAGE = "/app/20211014113026.jpg"

    image_data = open(TEST_IMAGE, "rb").read()
    data = {
        "ImageName": os.path.basename(TEST_IMAGE), # 图像名称，必填，用于获取结果。
        "Base64": False, # 是否返回图像base64, 默认True
        "RenderURL": True, # 是否返回检测结果渲染后的图像URL, 默认False, 因为写硬盘耽误接口时间
        # "ResultSaveDays": 3, # 结果图片保留时长, 默认3天，大于3天删除
    }
    # files 字段是必选择的
    response = requests.post(DETECTION_URL, files={"image": image_data}, data=data).json()

    # pprint.pprint(response)
    print(json.dumps(response, indent=4))

# 测试 electronic fence的【增改】【查】，暂时不允许删除
def test_electronic_fence_getall():
    # 测试获取全量电子围栏配置信息
    url = BASE_URL + '/v1/eletronic/getall'
    data = {}
    response = requests.post(url, data=data).json()
    print("begin"*1000)
    print(f"-------/v1/eletronic/getall")
    print(json.dumps(response, indent=4))
    print("*"*100)

def test_electronic_fence_get(x=None):
    # 测试获取个别电子围栏信息
    url = BASE_URL + '/v1/electronic/get'
    data = {
        'ele_fence_key': x
    }
    print('-------/v1/electronic/get')
    response = requests.post(url, data=json.dumps(data))
    print(response)
    print('-------/v1/electronic/get')
    print(response.json())
    # print(json.dumps(response, indent=4))
    print("*"*100)

def test_electronic_fence_update_or_add(x='xxxxxxxxxx', y=None):
    # 测试获取个别电子围栏信息
    url = BASE_URL + '/v1/electronic/update-or-add'
    data = {
        'ele_fence_key': x,
        'ele_fence_value': {
            'Description': "dam xxxx 测试",
            'PointsList': [[959, 753 ], [958, 776 ], [1099, 824 ], [1070, 829 ], [1081, 841 ], [1068, 846 ], [1091, 854 ], [1177, 858 ], [1220, 873 ], [1359, 912 ], [1356, 925 ], [1244, 955 ], [1546, 1054 ], [1705, 1328 ], [1754, 1286 ], [1841, 1309 ], [1867, 1388 ], [2179, 1439 ], [2556, 1436 ], [2556, 771 ], [1951, 761 ], [1839, 739 ], [1721, 742 ], [1475, 762 ], [1076, 759 ] ]
        }
    }
    response = requests.post(url, data=json.dumps(data)).json()
    print(f"------/v1/electronic/update-or-add")
    print(json.dumps(response, indent=4))
    print("*"*100)
    print("end"*1000)

def test_electronic_fence():
    test_electronic_fence_getall()
    test_electronic_fence_get(x="xxxxxxxxxx")
    test_electronic_fence_update_or_add()
    test_electronic_fence_getall()



if __name__ == "__main__":    
    test_electronic_fence()
    test_outside_example()
    test_ele_fence_example()